package com.fortune.service.impl;

import com.fortune.dao.ClientUserDao;
import com.fortune.dto.in.clientOrder.AddProduct;
import com.fortune.dto.in.clientOrder.ClientOrderEditDto;
import com.fortune.dto.in.clientOrder.ListDto;
import com.fortune.dto.in.clientOrder.AddDto;
import com.fortune.entity.*;
import com.fortune.service.ClientUserService;
import com.fortune.vo.clientOrder.DriverInfo;
import com.fortune.vo.clientOrder.OrderDetail;
import com.fortune.vo.clientOrder.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClientUserServiceImpl implements ClientUserService {
    @Autowired
    private ClientUserDao clientUserDao;
    @Override
    public List clientOrderList(ListDto listDto,String mid,String did) {
        List<Order> clientOrderList=clientUserDao.selectClientList(listDto,mid,did);
        if(clientOrderList.size() == 0){
            return null;
        }
        List list=new ArrayList();
        List<ProductOfUser> aa=clientUserDao.selectProduct(listDto);
        String description="商品名称："+aa.get(0).getName()+"等"+aa.size()+"种商品";
        for(Order order:clientOrderList){
            order.setDescription(description);
        }
        list.add(clientOrderList);
        return list;
    }

    @Override
    public int addProductOrder(AddDto addDto) {
        return clientUserDao.addOrder(addDto);
    }

    @Override
    public int clientOrderEdit(int id,int clientId,ClientOrderEditDto editDto) {
//       List<AddProduct> edit=clientUserDao.selectClientCount(editDto);
            int aa=clientUserDao.deleteOrder(id, clientId);
            if (aa>0){
                Integer num = clientUserDao.updateOrder(editDto);
            }
             List<AddProduct> asd =  editDto.getProducts();
     return 0;
    }

    @Override
    public int addDOrder(String mid, String did, String clientId, String id, String no, String userId, BigDecimal pTotalPrice, BigDecimal dTotalPrice) {
        return clientUserDao.addCOrder(mid, did, clientId, id, no, userId, pTotalPrice, dTotalPrice);
    }

    @Override
    public int addDOrderDetails(String mid, String did, String clientId, String id, int detailsNo, String productId, float orderNum, BigDecimal price, String policyId, BigDecimal unitPrice, float backBoxNum, BigDecimal dprice, BigDecimal tprice) {
        return clientUserDao.addCOrderDetails(mid, did, clientId, id, detailsNo, productId, orderNum, price, policyId, unitPrice, backBoxNum, dprice, tprice);
    }

    @Override
    public int addDOrderDetailsByZS(String mid, String did, String clientId, String id, int detailsNo, String productId, float orderNum, BigDecimal price, String policyId, BigDecimal unitPrice, float backBoxNum, BigDecimal dprice, BigDecimal tprice) {
        return clientUserDao.addCOrderDetailsByZS(mid, did, clientId, id, detailsNo, productId, orderNum, price, policyId, unitPrice, backBoxNum, dprice, tprice);
    }

    @Override
    public int editOrder(String mid, String did, String clientId, String ordersId, String ordersNums) {
        return clientUserDao.editOrder(mid, did, clientId, ordersId, ordersNums);
    }

    @Override
    public int updateOrderStatus(String orderId) {
        return clientUserDao.updateOrderStatus(orderId);
    }

    @Override
    public OrderDetail clientOrderDetail(String orderId) {
        OrderDetail clientOrderDetail=clientUserDao.selectOrder(orderId);
        DriverInfo driverInfo=clientUserDao.selectDriver(orderId);
        clientOrderDetail.setDriver(driverInfo);
        List<Product> products=clientUserDao.selectProductDetail(orderId);
        clientOrderDetail.setProducts(products);
        BigDecimal cc = new BigDecimal(0);
        int num = 0;
        BigDecimal dd = new BigDecimal(0);
        BigDecimal aa;
        for(int i=0; i<products.size();i++){
            Product product = products.get(i);
            cc = cc.add(product.getTotalPrice());
            num = num + product.getBackBoxNum();
            aa = new BigDecimal(product.getBackBoxNum());
            dd = dd.add(product.getDprice().multiply(aa));
        }
        clientOrderDetail.setdTotalPriceReduce(dd);
        clientOrderDetail.setTotalPrice(cc);
        clientOrderDetail.setTotalBackBoxNum(num);

        return clientOrderDetail;
    }
}
